#include "../h/DateList.h"
#include "../h/Event.h"
#include <iostream.h>//dell
using namespace std;//dell
#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"
using namespace Poco;

DateList::DateList(string name_)
{
	this->name = name_;
	eventsList = vector<Event*>();
	countTesting =1;
}

DateList::~DateList()
{
	this -> clear();
}

void DateList::clear()
{
	vector<Event*>::iterator it;
	//cout<<"i am in the clear for the "<<countTesting<<endl;
	countTesting++;
	int index =1;
	for(it= eventsList.begin();it < eventsList.end();)
	{
		//cout<<"the index is"<<index<<endl;
		index++;
		Event* event = *it;
		delete(event);
		event =0;
		eventsList.erase(it);
	}
}


void DateList::addSellingEvent(Event* e)
{
	this -> sortLowFirst(e);
}
void DateList::addBuyingEvent(Event* e)
{
	this -> sortHighFirst(e);
}
void DateList::addCCEvent(Event* e)
{
	this -> sortByDate(e);
}

Event DateList::getFirst(DateTime today)
{
	if((eventsList.size()>0)&&(today==eventsList.at(0)->getDate()))
	{
		Event ans = *eventsList.at(0);
		delete(eventsList.at(0));
		eventsList.erase(eventsList.begin());
		return ans;
	}
	return Event();
}

//this is for buyimg
void DateList::sortHighFirst(Event* e)
{
	if(eventsList.size()==0)
		eventsList.push_back(e);
	else
	{
		vector<Event*>::iterator it;
		it = eventsList.begin();
		Event* j = *it;
		for( it = eventsList.begin() ; (it < eventsList.end())&&(e->getDate()>j->getDate()); it++)
			if (it+1<eventsList.end())
				j= *(it+1);
			//j=*it;
		for( ; (it < eventsList.end())&&(j->getDate()==e->getDate() )&&(e->getPrice()<j->getPrice()); it++)
			if (it+1<eventsList.end())
				j= *(it+1);
			//j=*it;
		for( ; (it < eventsList.end())&&(j->getDate()==e->getDate() )&&(e->getPrice()==j->getPrice())&&(e->getUser()>j->getUser()); it++)
			if (it+1<eventsList.end())
				j= *(it+1);
		eventsList.insert(it,e);
	}
}

void DateList::sortByDate(Event* e)
{
	if(eventsList.size()==0)
		eventsList.push_back(e);
	else
	{
		vector<Event*>::iterator it;
		it = eventsList.begin();
		Event* j = *it;
		for( it = eventsList.begin() ; (it < eventsList.end())&&(e->getDate()>j->getDate()); it++ ) //i change it because it made problems
			if (it+1<eventsList.end())
				j= *(it+1);
		eventsList.insert(it,e);
	}
}

// this is for selling
void DateList::sortLowFirst(Event* e)
{
	if(eventsList.size()==0)
		eventsList.push_back(e);
	else
	{
		vector<Event*>::iterator it;
		it = eventsList.begin();
		Event* j = *it;
		for( it = eventsList.begin() ; (it < eventsList.end())&&(e->getDate()>j->getDate()); it++)
			if (it+1<eventsList.end())
							j= *(it+1);
		for( ;it < eventsList.end()&& (j->getDate()==e->getDate() )&&(e->getPrice()>j->getPrice()); it++)
			if (it+1<eventsList.end())
				j= *(it+1);
		for( ;it < eventsList.end()&& (j->getDate()==e->getDate() )&&(e->getPrice()==j->getPrice())&&(e->getUser()>j->getUser()); it++)
			if (it+1<eventsList.end())
				j= *(it+1);
		eventsList.insert(it,e);
	}
}




void DateList::print()
{
	vector<Event*>::iterator it;
	for(it = eventsList.begin(); it < eventsList.end(); it++)
	{
		//cout<<"printer dateList"<<endl;
		Event* j = *it;
		j-> print();
	}
}
string DateList::getName()
{
	return name;
}
void DateList::addEvent(Event* e)
{
	if(e->getType()=="sell")
		this->addSellingEvent(e);
	else if(e->getType()=="buy")
		this->addBuyingEvent(e);
	else if(e->getType()=="create")
		this->sortByDate(e);
	else if(e->getType()=="consume")
		this->sortByDate(e);
	else if(e->getType().find("Report")<50)
		this->sortByDate(e);
	//e->print();
}
DateTime DateList::getFirstDate()
{
	if (eventsList.size()>0)
		return (eventsList.at(0)->getDate());
	return DateTime(9999,12,31);
}

bool DateList::noMoreEvents()
{
	return (eventsList.size()==0);
}
